// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.

package nl.justobjects.pushlet.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * Implementation of ClientAdapter that sends Events as serialized objects.
 *
 * @version $Id: JavaPushletAdapter.java,v 1.3 2003/08/15 08:37:40 justb Exp $
 * @author Just van den Broecke - Just Objects &copy;
 */
class JavaPushletAdapter implements ClientAdapter {
	ObjectOutputStream out = null;

	public void start() throws IOException {
	}

	public void init(HttpServletRequest theHttpServletRequest,
					 HttpServletResponse theHttpServletResponse) throws IOException {

//		System.out.println("JavaPushletAdapter PROTOCOL=" + theHttpServletRequest.getProtocol());
//		System.out.println("JavaPushletAdapter CONNECTION=" + theHttpServletRequest.getHeader("Connection"));

		// The connection should be closed after this request
		// NB: this allows sending a "long response". Some clients
		// in particular java.net.URL in VMs > 1.1 that use HTTP/1.1
		// will block if
		// - the content length is not sent
		// - if Connection: close HTTP header is not sent.
		//
		// Since we don't know the content length we will assume
		// the underlying servlet engine will use chunked encoding.
		theHttpServletResponse.setHeader("Connection", "close");

		theHttpServletResponse.setContentType("application/x-java-serialized-object");

		// Use a serialized object output stream
		out = new ObjectOutputStream(theHttpServletResponse.getOutputStream());
	}

	public void push(Event event) throws IOException {
		out.writeObject(event);
		out.flush();
	}

	public void stop() throws IOException {
	}
}

/*
 * $Log: JavaPushletAdapter.java,v $
 * Revision 1.3  2003/08/15 08:37:40  justb
 * fix/add Copyright+LGPL file headers and footers
 *
 * Revision 1.2  2003/05/18 16:13:48  justb
 * fixed blocking for java.net.URL with HTTP/1.1 (JVMs > 1.1)
 *
 * Revision 1.1.1.1  2002/09/24 21:02:31  justb
 * import to sourceforge
 *
 * Revision 1.1.1.1  2002/09/20 22:48:18  justb
 * import to SF
 *
 * Revision 1.1.1.1  2002/09/20 14:19:03  justb
 * first import into SF
 *
 * Revision 1.5  2002/04/15 20:42:41  just
 * reformatting and renaming GuardedQueue to BlockingQueue
 *
 * Revision 1.4  2000/12/27 22:39:35  just
 * no message
 *
 * Revision 1.3  2000/08/21 20:48:29  just
 * added CVS log and id tags plus copyrights
 *
 *
 */
